package com.ruoyi.web.controller.blogs;

import com.ruoyi.blogs.domain.BsBanner;
import com.ruoyi.blogs.service.IBsBannerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Banner管理Controller
 *
 * @author ruoyi
 * @date 2022-10-28
 */
@RestController
@RequestMapping("/blogs/banner")
public class BsBannerController extends BaseController {
    @Autowired
    private IBsBannerService bsBannerService;

    /**
     * 查询Banner管理列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(BsBanner bsBanner) {
        startPage();
        List<BsBanner> list = bsBannerService.selectBsBannerList(bsBanner);
        return getDataTable(list);
    }

    /**
     * 导出Banner管理列表
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:export')")
    @Log(title = "Banner管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsBanner bsBanner) {
        List<BsBanner> list = bsBannerService.selectBsBannerList(bsBanner);
        ExcelUtil<BsBanner> util = new ExcelUtil<BsBanner>(BsBanner.class);
        util.exportExcel(response, list, "Banner管理数据");
    }

    /**
     * 获取Banner管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(bsBannerService.selectBsBannerById(id));
    }

    /**
     * 新增Banner管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:add')")
    @Log(title = "Banner管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsBanner bsBanner) {
        bsBanner.setId(IdUtils.fastSimpleUUID());
        return toAjax(bsBannerService.insertBsBanner(bsBanner));
    }

    /**
     * 修改Banner管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:edit')")
    @Log(title = "Banner管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsBanner bsBanner) {
        return toAjax(bsBannerService.updateBsBanner(bsBanner));
    }

    /**
     * 删除Banner管理
     */
    @PreAuthorize("@ss.hasPermi('blogs:banner:remove')")
    @Log(title = "Banner管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bsBannerService.deleteBsBannerByIds(ids));
    }
}
